<template>
  <div class="changpwd" style="margin-top:5vh;">
    <van-cell-group>
      <van-field v-model="username"  disabled label="账号" placeholder="请输入账号" />
      <van-field v-model="oldpassword" type="password" required clearable label="旧密码" placeholder="请输入旧密码" />
      <van-field v-model="newpassword" type="password" required clearable label="新密码" placeholder="请输入新密码" />
      <van-field v-model="confirmpassword" type="password" required clearable label="确认密码" placeholder="请再输入一次密码" />
    </van-cell-group>
    <van-button type="info" size="large" style="margin-top:5vh;" @click="changePassword">确认</van-button>
  </div>
</template>

<script>
import Vue from 'vue'
import { Cell, PullRefresh, Toast, Tabbar, TabbarItem, Popup } from 'vant'
import qs from 'qs'
Vue.use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup)
export default {
  name: 'changpwd',
  data () {
    return {
      oldpassword: '',
      newpassword: '',
      confirmpassword: '',
      username: this.$route.query.username
    }
  },
  methods: {
    changePassword () {
      let data = { current_password: this.oldpassword,
        new_password: this.newpassword,
        confirm_password: this.confirmpassword
      }
      // 如果是迪诺,走他们的接口
     const projectData = require('../project.json')
      //console.log(projectData)
      if (projectData.project === 'dinuo/') {
          fetch('https://passport.hnsendi.cn/auth/realms/dinuo/user-common-resource/changePassword', { method: 'post',
credentials: 'include',
body: new URLSearchParams(data),
headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  } }).then(res => res.json()).then(x => { if (x.code == 500) { Toast(x.message) } else Toast('修改成功!') })
     this.$router.push({ name: 'main' })
        return
      }
// 走之前的代码
       data = { oldpassword: this.oldpassword,
        newpassword: this.newpassword,
        confirmpassword: this.confirmpassword
      }
      this.axios({
        method: 'post',
        url: '/api/loginuser.changepwsave.do',
        data: qs.stringify(data)
      }).then(res => {
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') > 0) {
            this.loginout()
          }
        } else {
          Toast('修改密码成功！')
          this.$router.push({ name: 'main' })
        }
      }).catch(res => {
        this.reserror(res)
      })
    }
  }
}
</script>

<style>
.fieldgroup_title {
    text-align: left;
    margin: 0px;
    padding: 35px 15px 15px;
    color: rgba(69,90,100,0.6);
    font-weight: normal;
    font-size: 14px;
}
</style>
